#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <errno.h>

using namespace std;

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFD); // 获取文件描述符
    if (fl < 0)
    {
        perror("fcntl error");
        exit(0);
    }

    fcntl(fd, F_SETFL, fl | O_NONBLOCK); // 设置文件的属性
}

int main()
{
    SetNonBlock(0);
    char buffer[1024];
    for (;;)
    {
        cout << "please enter: "<<endl;
        fflush(stdout);
        int n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n - 1] = 0; // 干掉'\n'
            cout << buffer << endl;
        }
        else if (n == 0)
        {
            cout << "over " << endl;
            break;
        }
        else
        {
            perror("read");
            printf("%d \n",n);
            cout<<strerror(errno)<<endl;
            break;
        }
    }
    return 0;
}